package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.Thymeleaf;
import org.thymeleaf.context.Context;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
public class IndexController {
    @Autowired
    private ProductFeignClient productFeignClient;

    //首页 http://www.gmall.com/
    @GetMapping("/")
    public String index(Model model) {
        List<Map> list = getData();
        model.addAttribute("list", list);
        return "index/index";//springmvc整合了Thymeleaf
    }

    private List<Map> getData() {
        List<Map> list = new ArrayList<>();
        List<BaseCategoryView> baseCategoryViewList = productFeignClient.getBaseCategoryViewList();

        Map<Long, List<BaseCategoryView>> listMapByCategory1Id = baseCategoryViewList.stream().collect(
                Collectors.groupingBy((baseCategoryView -> {
                    return baseCategoryView.getCategory1Id();
//                    BaseCategoryView::getCategory1Id
                })));
        int index = 0;
        for (Map.Entry<Long, List<BaseCategoryView>> longListEntryByCategory1Id : listMapByCategory1Id.entrySet()) {
            List<BaseCategoryView> valueByCategory1Id = longListEntryByCategory1Id.getValue();
            HashMap mapByCateGory1Id = new HashMap<>();
            mapByCateGory1Id.put("index", ++index);
            mapByCateGory1Id.put("categoryId", longListEntryByCategory1Id.getKey());
            mapByCateGory1Id.put("categoryName", valueByCategory1Id.get(0).getCategory1Name());

            List<Map> listMap = new ArrayList<>();
            Map<Long, List<BaseCategoryView>> listMapByCategory2Id = valueByCategory1Id.stream().collect(
                    Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            for (Map.Entry<Long, List<BaseCategoryView>> longListEntryByCategory2Id : listMapByCategory2Id.entrySet()) {
                List<BaseCategoryView> valueByCategory2Id = longListEntryByCategory2Id.getValue();
                Map mapByCateGory2Id = new HashMap<>();
                mapByCateGory2Id.put("categoryId", longListEntryByCategory2Id.getKey());
                mapByCateGory2Id.put("categoryName", valueByCategory2Id.get(0).getCategory2Name());
                ///更改泛型///
                List<Map> collect = valueByCategory2Id.stream().map(baseCategoryView -> {
                    Map map = new HashMap<>();
                    map.put("categoryId", baseCategoryView.getCategory3Id());
                    map.put("categoryName", baseCategoryView.getCategory3Name());
                    //更改了泛型   List<BaseCategoryView> -> List<Map>
                    return map;
                }).collect(Collectors.toList());
                mapByCateGory2Id.put("categoryChild", collect);
                listMap.add(mapByCateGory2Id);
            }
            mapByCateGory1Id.put("categoryChild", listMap);
            list.add(mapByCateGory1Id);
        }
        return list;
    }

    @Autowired
    private TemplateEngine templateEngine;

    //人为触发 生成静态化的页面
    @GetMapping("/createHtml")
    @ResponseBody
    public Result createHtml() {
        //静态化页面过程
        //1.模板
        String templateName = "index/index";
        //2.数据
        List<Map> list = getData();
        Context context = new Context();
        context.setVariable("list", list);
        //3.输出流
        Writer out = null;
        try {
            //写 考虑编码问题
//            out = new OutputStreamWriter(new FileOutputStream("D:\\WLin\\sph_index\\index1.html"), "UTF-8");
            out = new PrintWriter("D:\\WLin\\sph_index\\index1.html", "UTF-8");
            //读 UTF-8
            templateEngine.process(templateName, context, out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != out){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return Result.ok();
    }
}
